import Vue from 'vue'
import VueRouter from 'vue-router'

// const Foo = () => import(/* webpackChunkName: "group-foo" */ './Foo.vue')
const Login = () =>
    import ( /* webpackChunkName: "Login_Home_Welcome" */ '../components/Login.vue')
const Home = () =>
    import ( /* webpackChunkName: "Login_Home_Welcome" */ '../components/Home.vue')
const Welcome = () =>
    import ( /* webpackChunkName: "Login_Home_Welcome" */ '../components/Welcome.vue')

const Users = () =>
    import ( /* webpackChunkName: "Users_Rights_Roles" */ '../components/users/Users.vue')
const Rights = () =>
    import ( /* webpackChunkName: "Users_Rights_Roles" */ '../components/power/Rights.vue')
const Roles = () =>
    import ( /* webpackChunkName: "Users_Rights_Roles" */ '../components/power/Roles.vue')

const Categories = () =>
    import ( /* webpackChunkName: "Categories_Goods_Add" */ '../components/goods/Categories.vue')
const Goods = () =>
    import ( /* webpackChunkName: "Categories_Goods_Add" */ '../components/goods/Goods.vue')
const Add = () =>
    import ( /* webpackChunkName: "Categories_Goods_Add" */ '../components/goods/Add.vue')

const Orders = () =>
    import ( /* webpackChunkName: "Orders_Reports" */ '../components/orders/Orders.vue')
const Reports = () =>
    import ( /* webpackChunkName: "Orders_Reports" */ '../components/reports/Reports.vue')



Vue.use(VueRouter)

const routes = [
    { path: '/login', component: Login },
    { path: '/', redirect: '/login' },
    {
        path: '/home',
        component: Home,
        redirect: '/welcome',
        children: [
            { path: '/welcome', component: Welcome },
            { path: '/users', component: Users },
            { path: '/rights', component: Rights },
            { path: '/roles', component: Roles },
            { path: '/categories', component: Categories },
            { path: '/goods', component: Goods },
            { path: '/goods/add', component: Add },
            { path: '/orders', component: Orders },
            { path: '/reports', component: Reports },
        ]
    },
];

const router = new VueRouter({
    routes
});

// 挂载路由守卫
router.beforeEach((to, from, next) => {
    // to and from are both route objects. must call `next`.
    // to是将要访问的地址
    // from代表哪里来的地址
    // next() 代表放行   next('/login') 代表跳转到该页面
    if (to.path === '/login') return next()
    const tokenStr = window.sessionStorage.getItem('token')
    if (!tokenStr) return next('/login')
    next()
})

export default router